﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class LeaveWord : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBound();
            CKEditorControl1.config.toolbar = "Basic";
        }
        if (Session["se1"] == null)
        {
            LabUserName.Text = "游客";
        }
        else
        {
            LabUserName.Text = Session["se1"].ToString();
        }
        //编辑和删除
        int lid;//操作对象的laeveID
        string own;//操作对象用户ID
        string change;//操作类型
        if (Request.QueryString["lid"] != null)
        {
            own = Request.QueryString["own"];
            lid = int.Parse(Request.QueryString["lid"]);
            change = Request.QueryString["act"];
            Change(change,lid,own);
        }


    }
    /// <summary>
    /// 绑定数据库
    /// </summary>
    protected void DataBound()
    {
        int num;
        int pageIndex = AspNetPager1.CurrentPageIndex;
        int pageSize = AspNetPager1.PageSize;
        DataSet ds = new DataSet();
        ds = new Xqsys.BLL.LeaveWork().SelectAllWord(pageIndex,pageSize,out num,"@count");//num为输出留言总数
        AspNetPager1.RecordCount = num;
        if (ds != null)
        {
            Repeater1.DataSource=ds;
            Repeater1.DataBind();
        }
    }  
     /// <summary>
     /// 分页控件
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        DataBound();
    }
    /// <summary>
    /// 提交文本
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void submit_Click(object sender, EventArgs e)
    {

        if ("游客" == LabUserName.Text.ToString())
        {
            Response.Write("<script>alert('请先登录！')</script>");
        }
        else if (""==TxtCode.Text)
        {
            LabError.Text = "请输入验证码！";
            LabError.Visible = true;
        }
        else if (false==code())//验证码
        {
            LabError.Text = "验证码输入错误！";
            LabError.Visible = true;
        }
        else 
        {
            sendIn();
        }
    }
    /// <summary>
    /// 验证码
    /// </summary>
    /// <returns></returns>
    protected bool code()
    {
        try
        {
            string text = TxtCode.Text.Trim().ToString();
            string sess;
            sess = (string)HttpContext.Current.Session["AuthenticationCode"];
            sess = sess.ToLower();
            text = text.ToLower();
            if (text == sess)
            { return true; }
            else
            { return false; }
        }
        catch
        {
            return false;
        }
    }
    /// <summary>
    /// 绑定数据
    /// </summary>
    protected void sendIn()
    {
        Xqsys.MODEL.LeaveWords leaveWord = new Xqsys.MODEL.LeaveWords();
        leaveWord.Content = CKEditorControl1.Text.Trim().ToString();
        leaveWord.Intime = DateTime.Now;
        leaveWord.UserName = LabUserName.Text.ToString();
        if (CKEditorControl1.Text.Trim().Length == 0)
        {
            Response.Write("<script>alert('留言不能为空！')</script>");
        }
        else if (new Xqsys.BLL.LeaveWork().AddLeaveWord(leaveWord))
        {
            Response.Write("<script>alert('发表成功！');window.location.href='LeaveWord.aspx';</script>");
        }
        else
        {
            Response.Write("<script>alert('发表失败！')</script>");
        }
    }
    /// <summary>
    /// 判断编辑和删除
    /// </summary>
    /// <param name="change">操作类型</param>
    /// <param name="lid">操作对象留言号</param>
    protected void Change(string change,int lid,string own)
    {
        //对管理员的判断
        string Changer;//操作者
        Changer= LabUserName.Text.ToString();
        if ("游客" ==Changer||Changer!=own)
        {
            Response.Write("<script>alert('抱歉！请登录或者您权限不够！')</script>");// 在没点确定前循环了一次
        }
        else
        {
            string userId;
            userId = Session["se1"].ToString();
            if ("edit" == change)
            {
                edit(lid,userId);
            }
            else if ("del" == change)
            {
                del(lid,userId);
            }
        }
    }
    /// <summary>
    /// 删除操作
    /// </summary>
    /// <param name="lid">删除的留言号</param>
    /// <param name="userId">操作者id</param>
    protected void del(int lid, string userId)
    {
      
        if (new Xqsys.BLL.LeaveWork().DeleteLeaveWord(lid))
        {
            Response.Write("<script>alert('删除成功！');window.location.href='LeaveWord.aspx';</script>");
        }
        else 
        {
            Response.Write("<script>alert('删除失败！');window.location.href='LeaveWord.aspx';</script>");
        }
    }
    /// <summary>
    /// 编辑操作
    /// </summary>
    /// <param name="lid">编辑留言号</param>
    /// <param name="userId">操作者id</param>
    protected void edit(int lid,string userId)
    {
        
    }
}